from typing import Optional,List
from fastapi import Depends
from ..depends.user import get_is_system_admin,get_is_system_or_all_admin
from ..typing.user import UserInDB
from ..app import app
from ..typing.dorm_administrator import Dorm_AdministratorInDB,dorm_administrator_table
from ..data import db
from ..typing.student import student_table

@app.get('/system_administrator/get_all_dorm',tags=['system_administrator'])
async def get_all_dorm(
    dorm_administrator_id:Optional[str]=None,
    d_ad_name:Optional[str]=None,
    bel_b_id:Optional[str]=None,
    user:UserInDB=Depends(get_is_system_or_all_admin)
)->List[Dorm_AdministratorInDB]:
    sel=dorm_administrator_table.select()
    if dorm_administrator_id:
        sel=sel.where(dorm_administrator_table.c.dorm_administrator_id==dorm_administrator_id)
    if d_ad_name:
        sel=sel.where(dorm_administrator_table.c.d_ad_name==d_ad_name)
    if bel_b_id:
        sel=sel.where(dorm_administrator_table.c.bel_b_id==bel_b_id)
    obj=await db.fetch_all(sel)
    return obj


@app.get('/system_administrator/update_student',tags=['system_administrator'])
async def update_all_student(
    s_id:Optional[str]=None,
    sname:Optional[str]=None,
    sex:Optional[str]=None,
    school:Optional[str]=None,
    grade:Optional[int]=None,
    now_class:Optional[int]=None,
    phone_number:Optional[str]=None,
    bel_b_name:Optional[str]=None,
    bel_d_number:Optional[str]=None,
    user:UserInDB=Depends(get_is_system_admin)
):
    await db.execute(student_table.update(
        student_table.c.s_id==s_id,
        {
            's_id':s_id,
            'sname':sname,
            'sex':sex,
            'school':school,
            'grade':grade,
            'now_class':now_class,
            'phone_number':phone_number,
            'bel_b_name':bel_b_name,
            'bel_d_number':bel_d_number,
        }
    ))
    return {'msg':'you have update successfully'}


@app.get('/system_administrator/update_dorm',tags=['system_administrator'])
async def update_all_dormitory(
    dorm_administrator_id:Optional[str]=None,
    d_ad_name:Optional[str]=None,
    sex:Optional[str]=None,
    bel_b_id:Optional[str]=None,
    user:UserInDB=Depends(get_is_system_admin)
):
    await db.execute(dorm_administrator_table.update(
        dorm_administrator_table.c.dorm_administrator_id==dorm_administrator_id,
        {
            'dorm_administrator_id':dorm_administrator_id,
            'd_ad_name':d_ad_name,
            'sex':sex,
            'bel_b_id':bel_b_id
        }
    ))
    return {'msg':'you have update successfully'}